package com.banciyuan.bcywebview.biz.main.group.groupselect;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.biz.groupdetail.NewTeamDetailActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.GroupWork;
import de.greenrobot.daoexample.model.Post;

/**
 * Created by squall on 2016/12/8.
 */

public class GroupBestAdapter extends BaseAdapter {
    private List<GroupWork> glist;
    protected Context mContext;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;

    public GroupBestAdapter(List<GroupWork> glist, Context mContext) {
        this.glist = glist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        this.imageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getItemViewType(int position) {
        GroupWork groupWork = glist.get(position);
        if (groupWork.getPosts().isEmpty()) {
            return 0;
        } else if (groupWork.getPosts().get(0).getMulti().isEmpty()) {
            return 0;
        } else if (groupWork.getPosts().get(0).getMulti().size() == 1) {
            return 1;
        } else if (groupWork.getPosts().get(0).getMulti().size() == 2) {
            return 2;
        } else if (groupWork.getPosts().get(0).getMulti().size() == 3) {
            return 3;
        } else {
            return 0;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getCount() {
        return glist.size();
    }

    @Override
    public Object getItem(int i) {
        return glist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        GroupViewNewHolder viewEmpty = null;
        GroupViewNewHolder viewOne = null;
        GroupViewNewHolder viewTwo = null;
        GroupViewNewHolder viewThree = null;

        if (convertView == null) {
            switch (getItemViewType(position)) {
                case 0:
                    convertView = inflater.inflate(R.layout.group_hot_item, null);
                    viewEmpty = new GroupViewNewHolder(convertView);
                    convertView.setTag(viewEmpty);
                    break;
                case 1:
                    convertView = inflater.inflate(R.layout.group_hot_item, null);
                    viewOne = new GroupViewNewHolder(convertView);
                    convertView.setTag(viewOne);
                    break;
                case 2:
                    convertView = inflater.inflate(R.layout.group_hot_item, null);
                    viewTwo = new GroupViewNewHolder(convertView);
                    convertView.setTag(viewTwo);
                    break;
                case 3:
                    convertView = inflater.inflate(R.layout.group_hot_item, null);
                    viewThree = new GroupViewNewHolder(convertView);
                    convertView.setTag(viewThree);
                    break;
                default:
                    convertView = inflater.inflate(R.layout.group_hot_item, null);
                    viewEmpty = new GroupViewNewHolder(convertView);
                    convertView.setTag(viewEmpty);
                    break;
            }
        } else {
            switch (getItemViewType(position)) {
                case 0:
                    viewEmpty = (GroupViewNewHolder) convertView.getTag();
                    break;
                case 1:
                    viewOne = (GroupViewNewHolder) convertView.getTag();
                    break;
                case 2:
                    viewTwo = (GroupViewNewHolder) convertView.getTag();
                    break;
                case 3:
                    viewThree = (GroupViewNewHolder) convertView.getTag();
                    break;
                default:
                    viewEmpty = (GroupViewNewHolder) convertView.getTag();
                    break;
            }
        }

        GroupWork groupWork = glist.get(position);
        switch (getItemViewType(position)) {
            case 0:
                renderGroup(viewEmpty, groupWork);
                break;
            case 1:
                renderGroup(viewOne, groupWork);
                break;
            case 2:
                renderGroup(viewTwo, groupWork);
                break;
            case 3:
                renderGroup(viewThree, groupWork);
                break;
        }

        return convertView;
    }

    private void renderGroup(GroupViewNewHolder viewNewHolder, final GroupWork groupWork) {
        if (!TextUtils.isEmpty(groupWork.getName()))
            viewNewHolder.groupTitle.setText(groupWork.getName());
        if (!groupWork.getPosts().isEmpty()) {
            viewNewHolder.content_container.setVisibility(View.VISIBLE);
            viewNewHolder.say_split.setVisibility(View.VISIBLE);
            Post post = groupWork.getPosts().get(0);
            viewNewHolder.plan.setText(Html.fromHtml(String.format(mContext.getString(R.string.best_answer_plain), post.getUname(), post.getPlain())));
            if (!post.getMulti().isEmpty()) {
                viewNewHolder.img_container.setVisibility(View.VISIBLE);
                for (int i = 0; i < post.getMulti().size(); i++) {
                    if (i < viewNewHolder.imageViews.length) {
                        imageLoader.displayImage(post.getMulti().get(i).getPath(), viewNewHolder.imageViews[i], ImageOptions.options);
                    }
                }
            } else {
                viewNewHolder.img_container.setVisibility(View.GONE);
            }
            if (Integer.parseInt(post.getPic_num()) >= 3) {
                viewNewHolder.pagenum_tv.setVisibility(View.VISIBLE);
                viewNewHolder.pagenum_tv.setText(post.getPic_num());
            } else {
                viewNewHolder.pagenum_tv.setVisibility(View.GONE);
            }
            viewNewHolder.content_container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DetailDataHelper.gotoGroupDetail(mContext, groupWork.getGid(), groupWork.getPosts().get(0).getPost_id(), false);
                }
            });
        } else {
            viewNewHolder.content_container.setVisibility(View.GONE);
            viewNewHolder.say_split.setVisibility(View.GONE);
        }
        ((View) (viewNewHolder.groupTitle.getParent())).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoTeamDetail(groupWork);
            }
        });
    }

    public void gotoTeamDetail(GroupWork groupWork) {
        gotoUtil.gotoAct(mContext, NewTeamDetailActivity.class, groupWork.getGid(), groupWork.getName());
    }

    private class GroupViewNewHolder {
        public TextView groupTitle;
        public ImageView[] imageViews;
        public TextView plan;
        public LinearLayout img_container;
        public LinearLayout say_split;
        public RelativeLayout content_container;
        public TextView pagenum_tv;

        public GroupViewNewHolder(View convertView) {
            groupTitle = (TextView) convertView.findViewById(R.id.tv_topic_title);
            imageViews = new ImageView[]{
                    (ImageView) convertView.findViewById(R.id.iv_picsone),
                    (ImageView) convertView.findViewById(R.id.iv_picstwo),
                    (ImageView) convertView.findViewById(R.id.iv_picsthree)};
            plan = (TextView) convertView.findViewById(R.id.tv_desc_one);
            img_container = (LinearLayout) convertView.findViewById(R.id.ll_pics_one);
            say_split = (LinearLayout) convertView.findViewById(R.id.say_line);
            content_container = (RelativeLayout) convertView.findViewById(R.id.content_container);
            pagenum_tv = (TextView) convertView.findViewById(R.id.pagenum_tv);
        }
    }

}
